package com.zys.asseter.controller;

import com.zys.asseter.constant.AssetUpdateCountConstant;
import com.zys.asseter.domain.dto.AssetFingerprintDTO;
import com.zys.asseter.domain.dto.AssetFingerprintInfoDTO;
import com.zys.asseter.domain.dto.AssetUpdateCountDto;
import com.zys.asseter.domain.pojo.AssetFingerprintInfoEntity;
import com.zys.asseter.domain.pojo.Result;
import com.zys.asseter.domain.vo.AssetFingerprintInfoVo;
import com.zys.asseter.domain.vo.ManufacturerCountVo;
import com.zys.asseter.domain.vo.TypeTreeVO;
import com.zys.asseter.mapper.AssetFingerprintInfoMapper;
import com.zys.asseter.service.AssetFingerprintInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@Validated
@Slf4j
@RequestMapping("/api/asm/asset-fingerprint")
public class AssetFingerprintInfoController {

    private final AssetFingerprintInfoService assetFingerprintInfoService;

    private final AssetFingerprintInfoMapper assetFingerprintInfoMapper;

    /**
     * 从资产信息表插入数据到资产指纹信息表
     *
     * @return
     */
    @PostMapping("/insert/data")
    public Result getData() {
        log.info("开始插入数据");
        assetFingerprintInfoService.insertDataFromAssetInfoToFingerInfo();
        return Result.success();
    }

    /**
     * 获取大类分类构建树形结构
     * @return
     */
    @GetMapping("/typeTree")
    public Result getTypeTree() {
        List<TypeTreeVO> typeTree = assetFingerprintInfoService.getTypeTree();
        return Result.success(typeTree);
    }
    /**
     * 根据大类和资分类查询指纹名称
     * @param assetType
     * @param assetSubType
     * @return
     */
    @GetMapping("/select/fingerName")
    public Result<List<String>> selectFingerName(
            @RequestParam("assetType") String assetType,
            @RequestParam("assetSubType") String assetSubType
    ){
        List<String> strings = assetFingerprintInfoService.selectFingerName(assetType, assetSubType);
        return  Result.success(strings);

    }


    /**
     * 查询所有厂商和对应资产表中的资产数量
     */
    @GetMapping("/select/manufacturer")
    public Result<List<ManufacturerCountVo>> selectManufacturer() {
        List<String> manufacturers = assetFingerprintInfoService.selectManufacturer();
        List<ManufacturerCountVo> manufacturerCounts = manufacturers.stream().map(manufacturer -> {
            String manufacturerCode = assetFingerprintInfoMapper.selectManufacturerCode(manufacturer);
            long count = assetFingerprintInfoMapper.countByManufacturer(manufacturerCode);
            ManufacturerCountVo mv = new ManufacturerCountVo();
            mv.setManufacturer(manufacturer);
            mv.setNum(count);
            return mv;
        }).collect(Collectors.toList());
        return Result.success(manufacturerCounts);
    }

    /**
     * 根据厂商查询指纹名称
     * @param manufacturer
     * @return
     */
    @GetMapping("/select/byManufacturer")
    public Result<List<String>> selectByManufacturer(@RequestParam String manufacturer) {
        List<String> strings = assetFingerprintInfoService.selectByManufacturer(manufacturer);
        return Result.success(strings);
    }


    /**
     * 根据指纹名称删除指纹信息
     * @param id
     * @return
     */

    @PostMapping("/delete/fingerInfo")
    public Result deleteFingerInfo(@RequestParam("id") String id) {
        assetFingerprintInfoService.deleteById(id);
        return Result.success();
    }



    /**
     * 资产表新增一条数据的时候判断对应的指纹在指纹表中是否存在
     */
    @GetMapping("/select/fingerCode")
    public Result judgeFingerInfo(@RequestParam String code) {
        Integer i = assetFingerprintInfoService.judgeFingerInfo(code);
        return Result.success(i);
    }


    /**
     * 根据指纹名称查询指纹信息
     * @param name
     * @return
     */
    @GetMapping("/select/FingerprintInfo")
    public Result<AssetFingerprintInfoVo> getAssetFingerprintInfo(@Param("name") String name) {
     AssetFingerprintInfoEntity info = assetFingerprintInfoService.getByName(name);
     AssetFingerprintInfoVo vo = new AssetFingerprintInfoVo();
     BeanUtils.copyProperties(info,vo);
     Integer num = assetFingerprintInfoMapper.countAssetsByFingerCode(info.getFingerNameCode());
     vo.setNum(num);
        return Result.success(vo);
    }

    /**
     * 新增指纹信息
     * @param
     * @return
     */
    @PostMapping("insert/fingerInfo")
    public Result insertFingerInfo(@RequestBody AssetFingerprintInfoDTO dto) {
        AssetFingerprintDTO fingerInfo = new AssetFingerprintDTO();
        fingerInfo.setManufacturer(dto.getManufacturer());
        fingerInfo.setModel(dto.getModel());
        fingerInfo.setBrand(dto.getBrand());
        Integer result = assetFingerprintInfoMapper.judge(fingerInfo);
        if(result==1){
            throw new RuntimeException("指纹信息已存在");
        }
        assetFingerprintInfoService.insertFingerInfo(dto);
        return  Result.success();
    }

    @GetMapping("/select/count")
    public Result getAssetFingerprintInfoMapper(
            @RequestParam String domainName
    ){
        if(StringUtils.isEmpty(domainName)){
            return Result.success();
        }
        Integer count = assetFingerprintInfoService.countDomainUsage(domainName);
        return Result.success(count);
    }

    @PostMapping(value = "/update/count")
    public Result updateCount(@RequestBody AssetUpdateCountDto assetUpdateCountDto) {
        if(assetUpdateCountDto.getDomainAddress() != null&&!assetUpdateCountDto.getDomainAddress().equals("")){
            assetFingerprintInfoService.updateCount(assetUpdateCountDto.getDomainAddress(), AssetUpdateCountConstant.DOMAIN_ADDRESS);
            return Result.success();
        }else if(assetUpdateCountDto.getIcpRecordNumber()!=null&&!assetUpdateCountDto.getIcpRecordNumber().equals("")){
            assetFingerprintInfoService.updateCount(assetUpdateCountDto.getIcpRecordNumber(),AssetUpdateCountConstant.ICP_RECORD_NAME);
            return Result.success();
        }else {
            return Result.error("域名或备案号为空,默认使用次数为0");
        }
    }

    @GetMapping("/init/count")
    public Result initCount(){
        assetFingerprintInfoService.initCount();
        return Result.success();
    }




}
